package com.baiyue.admin.controller;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.baiyue.admin.common.MessageResult;
import com.baiyue.admin.common.PermissionUtils;
import com.baiyue.admin.entity.AdminInfo;
import com.baiyue.admin.entity.Operation;
import com.baiyue.admin.manage.AdminInfoManage;


@Controller
public class AdminAct {
	private final static Logger LOGGER = Logger.getLogger(AdminAct.class);
	@Autowired
	private AdminInfoManage adminInfoManage;
	
	@RequestMapping("/admin/index.do")
	public ModelAndView index(){
		return new ModelAndView("/admin/index");
	}
	
	
	@RequestMapping("/admin/list.do")
	public void list(HttpServletRequest request,HttpServletResponse response) throws IOException{
		//验证
		MessageResult result = PermissionUtils.checkPermission(request, Operation.ADMIN_LIST, "");
		if(!result.isSuccess()){
			response.getWriter().write(result.marsh());
			response.getWriter().flush();
			return;
		}
		//取数据
		List<AdminInfo> results = adminInfoManage.getAllAdmin();
		if(results == null || results.size() ==0){
			result = new MessageResult(true,"没有找到数据");
		}else{
			result = new MessageResult(true,"找到数据");
			result.setResults(results);
		}
		response.getWriter().write(result.marsh());
		response.getWriter().flush();
	}
	@RequestMapping("/admin/deleteAdmin.do")
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException{
		//验证
		MessageResult result = PermissionUtils.checkPermission(request, Operation.BOOK_ADMIN_DELETE,"");
		if(!result.isSuccess()){
			response.getWriter().write(result.marsh());
			response.getWriter().flush();
			return;
		}
		//取数据
		String adminId  = request.getParameter("adminId");
		boolean success = adminInfoManage.deleteAdmin(adminId);
		result = new MessageResult();
		result.setSuccess(success);
		if(success){
			result.setMessage("删除成功");
		}else{
			result.setMessage("删除失败");
		}
		response.getWriter().write(result.marsh());
		response.getWriter().flush();
	}
	
	@RequestMapping("/admin/adminUpdate.do")
	public void update(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String adminId = request.getParameter("adminId");
		String name    = request.getParameter("name");
		String oldPassword= request.getParameter("oldPassword");
		String newPassword= request.getParameter("newPassword");
		String email   = request.getParameter("email");
		String role    = request.getParameter("role");
		MessageResult result = new MessageResult();
		System.out.println("adminUpdate.do:"+adminId+":"+name+":"+oldPassword+":"+newPassword+":"+email+":"+role);
		if(adminId == null || name == null || email == null ||role == null){
			result.setSuccess(false);
			result.setMessage("需要更新的管理员资料不齐全.");
			response.getWriter().write(JSONObject.toJSONString(result));
			response.getWriter().flush();
			return;
		}
		result = PermissionUtils.checkPermission(request, Operation.BOOK_ADMIN_UPDATE, oldPassword);
		if(!result.isSuccess()){
			response.getWriter().write(result.marsh());
			response.getWriter().flush();
			return;
		}
		AdminInfo old = adminInfoManage.selectById(adminId);
		if(old == null){
			result.setSuccess(false);
			result.setMessage("没有该管理员,无法更新.");
			response.getWriter().write(result.marsh());
			response.getWriter().flush();
			return;
		} else if(!old.getName().equals(name)){
			old = adminInfoManage.getAdminInfoByName(name);
			if(old != null){
				result.setSuccess(false);
				result.setMessage("该管理员名称已使用,请使用其他名称.");
				response.getWriter().write(result.marsh());
				response.getWriter().flush();
				return;
			}
		} else if(!old.getPassword().equals(oldPassword)){
			System.out.println("password:"+old.getPassword());
			result.setSuccess(false);
			result.setMessage("密码不正确.");
			response.getWriter().write(result.marsh());
			response.getWriter().flush();
			return;
		}
		AdminInfo adminInfo = new AdminInfo();
		adminInfo.setAdminId(adminId);
		adminInfo.setEmail(email);
		if(newPassword != null && newPassword !="" && !newPassword.equals("请输入密码(不修改密码无需)")){
			adminInfo.setPassword(newPassword);
		}else {
			adminInfo.setPassword(oldPassword);
		}
		adminInfo.setName(name);
		adminInfo.setRole(role);
		boolean success = adminInfoManage.updateAdmin(adminInfo);
		result.setSuccess(success);
		if(success){
			result.setMessage("更新成功");
		}else{
			result.setMessage("更新失败");
		}
		response.getWriter().write(result.marsh());
		response.getWriter().flush();
	}
	@RequestMapping("/admin/adminInsert.do")
	public void insert(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String name    = request.getParameter("name");
		String password= request.getParameter("newPassword");
		String email   = request.getParameter("email");
		String role    = request.getParameter("role");
		MessageResult result = new MessageResult();
		System.out.println(name+":"+password+":"+email+":"+role);
		if(name == null || password == null|| email == null ||role == null){
			result.setSuccess(false);
			result.setMessage("需要插入的管理员资料不齐全.");
			response.getWriter().write(JSONObject.toJSONString(result));
			response.getWriter().flush();
			return;
		}
		result = PermissionUtils.checkPermission(request, Operation.BOOK_ADMIN_INSERT, "");
		if(!result.isSuccess()){
			response.getWriter().write(result.marsh());
			response.getWriter().flush();
			return;
		}
		AdminInfo adminInfo = new AdminInfo();
		adminInfo.setAdminId(UUID.randomUUID().toString().replace("-", "").substring(0,12));
		adminInfo.setEmail(email);
		adminInfo.setPassword(password);
		adminInfo.setName(name);
		adminInfo.setRole(role);
		boolean success = adminInfoManage.insertAdmin(adminInfo);
		result.setSuccess(success);
		if(success){
			result.setMessage("插入成功");
		}else{
			result.setMessage("插入失败");
		}
		response.getWriter().write(result.marsh());
		response.getWriter().flush();
	}
}
